package com.buffered.homework;

import day09.Studen01;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author げっしょく
 * @date 2021/8/3
 */
public class Test4 {
    /*
     javase面向对象课程中讲解了一个学生管理系统，尝试将学生管理系统改为IO版本实现。
提示：其他的代码几乎都一样，就是将集合中存储的学生对象数据保存到本地文件中即可。
     */
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        int a = 0;
        ArrayList<Studen01> list = new ArrayList<>();
        io:
        while (true) {
            System.out.println("-------欢迎使用学生管理系统-------");
            System.out.println("1 增加学生信息");
            System.out.println("2 删除学生信息");
            System.out.println("3 修改学生信息");
            System.out.println("4 查看学生信息");
            System.out.println("5 将学生信息存入文档");
            System.out.println("6 退出");
            System.out.println("请输入你的选择：");
            a = sc.nextInt();
            switch (a) {
                case 1:
                    addStudent(list);
                    break;
                case 2:
                    deletestu(list);
                    break;
                case 3:
                    setStudent(list);
                    break;
                case 4:
                    printList(list);
                    break;
                case 5:
                    PrintFile(list);
                case  6:
                    break io;
                default:
                    System.out.println("你输入的数字错误！请重新输入：");
            }
        }
    }

    private static void PrintFile(ArrayList<Studen01> list) throws IOException {
        FileOutputStream fos = new FileOutputStream("D:\\stu.txt");
        for (Studen01 studen01 : list) {
            fos.write((studen01.toString().getBytes()) );
            fos.write("\r\n".getBytes());
        }
        fos.flush();
        fos.close();
    }

    public static String idset(ArrayList<Studen01> list , String id) {
        Studen01 stu2 = new Studen01();
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < list.size(); i++) {
            stu2 = list.get(i);
            if (id.equals(stu2.getId())) {
                System.out.println("您输入的id有误，请重新输入");
                id = sc.next();
                id = idset(list, id);
            }
        }
        return id;
    }
    public static void addStudent(ArrayList<Studen01> list) throws FileNotFoundException {
        Scanner sc = new Scanner(System.in);
        Studen01 stu1 = new Studen01();
        System.out.println("请输入要添加学生的姓名：");
        stu1.setName(sc.next());
        System.out.println("请输入要添加学生的学号：");
        String id = sc.next();
        id = idset(list, id);
        stu1.setId(id);
        System.out.println("请输入要添加学生的年龄：");
        stu1.setAge(sc.nextInt());
        list.add(stu1);
    }

    public static void printList(ArrayList<Studen01> list) {
        if (list.size() == 0) {
            System.out.println("管理系统中还没有数据，请先添入数据");
            return;
        }
        System.out.println("学生姓名\t学号\t年龄");
        for (int i = 0; i < list.size(); i++) {
            Studen01 stu = list.get(i);
            System.out.println(stu.getName() + "\t" + stu.getId() + "\t" + stu.getAge());
        }
    }

    public static Studen01 setStudent(ArrayList<Studen01> list) {
        Scanner sc = new Scanner(System.in);
        Studen01 stu1 = new Studen01();
//        System.out.println("请输入要修改信息学生的姓名：");
//        stu1.setName(sc.next());
        System.out.println("请输入修改信学生的学号：");
        stu1.setId(sc.next());
        for (int i = 0; i < list.size(); i++) {
            Studen01 stu2 = list.get(i);
            if (stu1.getId().equals(stu2.getId())) {
                System.out.println("请输入修改后学生的姓名：");
                stu2.setName(sc.next());
                System.out.println("请输入修改后学生的学号：");
                String id = sc.next();
                id = idset(list, id);
                stu2.setId(id);
                System.out.println("请输入修改后学生的年龄：");
                stu2.setAge(sc.nextInt());
                list.set(i, stu2);
            } else if (i == list.size() - 1) {
                System.out.println("你输入的学号不存在");
            }
        }
        return stu1;
    }
    public static void deletestu(ArrayList<Studen01> list){
        Scanner sc = new Scanner(System.in);
        Studen01 stu1 = new Studen01();
//        System.out.println("请输入要删除学生的姓名：");
//        String name = sc.next();
        System.out.println("请输入要删除学生的学号：");
        String id = sc.next();
//        System.out.println("请输入要删除学生的年龄：");
//        int age = sc.nextInt();
        for (int i = 0; i < list.size(); i++) {
            Studen01 stu = (Studen01) list.get(i);
            if(id.equals(stu.getId())){
                list.remove(i);
                return ;
            }
//            if(name.equals(stu.getName()) && id.equals(stu.getId()) && age == stu.getAge()){
//                list.remove(i);
//            }
        }
        System.out.println("你输入的学号不存在");
    }
}
